package com.hex.liyue.messagemodule.activity;

import android.content.Intent;
import android.os.Bundle;

import com.hex.liyue.R;
import com.hex.liyue.commonmodule.config.Constant;
import com.hex.liyue.commonmodule.manager.UserManager;
import com.hex.liyue.commonmodule.utils.StatusBarCompatUtils;
import com.hyphenate.easeui.ui.EaseBaseActivity;
import com.hyphenate.easeui.ui.EaseChatFragment;

public class ChatActivity extends EaseBaseActivity {
    public static ChatActivity activityInstance;
    private EaseChatFragment chatFragment;
    String toChatUsername;
    private String name,avator,fromName,fromAvatar;

    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        setContentView(R.layout.activity_chat);
        StatusBarCompatUtils.compat(this);
        activityInstance = this;
        name=getIntent().getStringExtra("name");
        avator= UserManager.getUserAvator();
        fromName=getIntent().getStringExtra("fromName");
        fromAvatar=getIntent().getStringExtra("fromAvatar");
        //聊天人或群id
        toChatUsername = getIntent().getExtras().getString(Constant.EXTRA_USER_ID);
        chatFragment = new EaseChatFragment();
        //传入参数
        chatFragment.setArguments(getIntent().getExtras());
        getSupportFragmentManager().beginTransaction().add(R.id.container, chatFragment).commit();
        chatFragment.setName(name,avator,fromName,fromAvatar);
    }

    public String getName(){
        return getIntent().getStringExtra("name");
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        activityInstance = null;
    }

    @Override
    protected void onNewIntent(Intent intent) {
        // 点击notification bar进入聊天页面，保证只有一个聊天页面
        String username = intent.getStringExtra("userId");
        if (toChatUsername.equals(username))
            super.onNewIntent(intent);
        else {
            finish();
            startActivity(intent);
        }

    }

    @Override
    public void onBackPressed() {
        chatFragment.onBackPressed();
    }

    public String getToChatUsername() {
        return toChatUsername;
    }
}
